//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FlatVol.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/PiecewiseConstantCorrelation.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/MarketModel.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (Cephei::IVector<Double>^ volatilities, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, UInt64 numberOfFactors, Cephei::IVector<Double>^ initialRates, Cephei::IVector<Double>^ displacements) : CMarketModel(CFlatVol::typeid)
{
    CoVector<Double>^ _Cvolatilities;
    CPiecewiseConstantCorrelation^ _Ccorr;
    CEvolutionDescription^ _Cevolution;
    CoVector<Double>^ _CinitialRates;
    CoVector<Double>^ _Cdisplacements;
    try
    {
#ifdef HANDLE
        _phFlatVol = NULL;
#endif
        CoVector<Double>^ _Cvolatilities = safe_cast<CoVector<Double>^> (volatilities);
        _Cvolatilities->Lock();
        INativeVector<Double>^ _NCIvolatilities = _Cvolatilities->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvolatilities = safe_cast<CDoubleVector^>(_NCIvolatilities);
        std::vector<QuantLib::Volatility>& _volatilities = static_cast<std::vector<QuantLib::Volatility>&> (_NCvolatilities->GetReference ());
        _Ccorr = safe_cast<CPiecewiseConstantCorrelation^> (corr);
        _Ccorr->Lock();
        boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>& _corr = static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>&> (_Ccorr->GetShared ()); 
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        QuantLib::Size _numberOfFactors = (QuantLib::Size)ValueHelper::Convert (numberOfFactors);
        CoVector<Double>^ _CinitialRates = safe_cast<CoVector<Double>^> (initialRates);
        _CinitialRates->Lock();
        INativeVector<Double>^ _NCIinitialRates = _CinitialRates->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCinitialRates = safe_cast<CDoubleVector^>(_NCIinitialRates);
        std::vector<QuantLib::Rate>& _initialRates = static_cast<std::vector<QuantLib::Rate>&> (_NCinitialRates->GetReference ());
        CoVector<Double>^ _Cdisplacements = safe_cast<CoVector<Double>^> (displacements);
        _Cdisplacements->Lock();
        INativeVector<Double>^ _NCIdisplacements = _Cdisplacements->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdisplacements = safe_cast<CDoubleVector^>(_NCIdisplacements);
        std::vector<QuantLib::Spread>& _displacements = static_cast<std::vector<QuantLib::Spread>&> (_NCdisplacements->GetReference ());
        _ppFlatVol = new boost::shared_ptr<QuantLib::FlatVol> (new QuantLib::FlatVol ( _volatilities,  _corr,  _evolution,  _numberOfFactors,  _initialRates,  _displacements ));
        SetMarketModel (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFlatVol));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cvolatilities != nullptr) _Cvolatilities->Unlock();
        if (_Ccorr != nullptr) _Ccorr->Unlock();
        if (_Cevolution != nullptr) _Cevolution->Unlock();
        if (_CinitialRates != nullptr) _CinitialRates->Unlock();
        if (_Cdisplacements != nullptr) _Cdisplacements->Unlock();
    }
}
Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (boost::shared_ptr<QuantLib::FlatVol>& childNative, Object^ owner) : CMarketModel(CFlatVol::typeid)
{
#ifdef HANDLE
	_phFlatVol = NULL;
#endif
	_ppFlatVol = &childNative;
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFlatVol));
}
Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (QuantLib::FlatVol& childNative, Object^ owner) : CMarketModel(CFlatVol::typeid)
{
#ifdef HANDLE
	_phFlatVol = NULL;
#endif
	_ppFlatVol = new boost::shared_ptr<QuantLib::FlatVol> (&childNative);
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFlatVol));
    _FlatVolOwner = owner;
    _MarketModelOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (CFlatVol^ copy) : CMarketModel(CFlatVol::typeid)
{
#ifdef HANDLE
	_phFlatVol = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFlatVol = new boost::shared_ptr<QuantLib::FlatVol> (copy->GetShared());
        _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFlatVol));
    }
}
Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (System::Type^ t) : CMarketModel(CFlatVol::typeid)
{
#ifdef HANDLE
	_phFlatVol = NULL;
#endif
	if (!t->IsSubclassOf(CFlatVol::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (QuantLib::Handle<QuantLib::FlatVol>& childNative, Object^ owner)  : CMarketModel(CFlatVol::typeid)
{
	_phFlatVol = &childNative;
	_ppFlatVol = &static_cast<boost::shared_ptr<QuantLib::FlatVol>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFlatVol));
    _FlatVolOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (QuantLib::Handle<QuantLib::FlatVol> childNative)  : CMarketModel(CFlatVol::typeid)
{
	_phFlatVol = &childNative;
	_ppFlatVol = &static_cast<boost::shared_ptr<QuantLib::FlatVol>>(childNative.currentLink());
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFlatVol));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CFlatVol::CFlatVol (QuantLib::FlatVol childNative)  : CMarketModel(CFlatVol::typeid)
{
#ifdef HANDLE
	_phFlatVol = NULL;
#endif
	_ppFlatVol = new boost::shared_ptr<QuantLib::FlatVol> (new QuantLib::FlatVol (childNative));
    _ppMarketModel = new boost::shared_ptr<QuantLib::MarketModel> (boost::dynamic_pointer_cast<QuantLib::MarketModel> (*_ppFlatVol));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CFlatVol::~CFlatVol ()
{
    if (_ppFlatVol != NULL)
    {
	    delete _ppFlatVol;
        _ppFlatVol = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CFlatVol::!CFlatVol ()
{
    if (_ppFlatVol != NULL)
    {
	    delete _ppFlatVol;
    }
}
QuantLib::FlatVol& Cephei::QL::Models::Marketmodels::Models::CFlatVol::GetReference ()
{
    if (_ppFlatVol == NULL) throw gcnew NativeNullException ();
	return **_ppFlatVol;
}
boost::shared_ptr<QuantLib::FlatVol>& Cephei::QL::Models::Marketmodels::Models::CFlatVol::GetShared ()
{
    if (_ppFlatVol == NULL) throw gcnew NativeNullException ();
	return *_ppFlatVol;
}
QuantLib::FlatVol* Cephei::QL::Models::Marketmodels::Models::CFlatVol::GetPointer ()
{
    if (_ppFlatVol == NULL) throw gcnew NativeNullException ();
	return &**_ppFlatVol;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FlatVol>& Cephei::QL::Models::Marketmodels::Models::CFlatVol::GetHandle ()
{
	if (_phFlatVol == NULL)
	{
		_phFlatVol = new Handle<QuantLib::FlatVol> (*_ppFlatVol);
	}
	return *_phFlatVol;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CFlatVol::HasNative () 
{
	return (_ppFlatVol != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CFlatVol::Displacements::get ()
{
    try
    {
    	std::vector<QuantLib::Spread>& _rv = (std::vector<QuantLib::Spread>&)(*_ppFlatVol)->displacements ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Models::CFlatVol::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppFlatVol)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = gcnew Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Models::Marketmodels::Models::CFlatVol::InitialRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppFlatVol)->initialRates ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFlatVol::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFlatVol)->numberOfFactors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFlatVol::NumberOfRates::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFlatVol)->numberOfRates ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Models::CFlatVol::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppFlatVol)->numberOfSteps ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IMatrix^ Cephei::QL::Models::Marketmodels::Models::CFlatVol::PseudoRoot (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	QuantLib::Matrix& _rv = (QuantLib::Matrix&)(*_ppFlatVol)->pseudoRoot ( _i );   
        Cephei::QL::Math::CMatrix^ _nrv = gcnew Cephei::QL::Math::CMatrix (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::IFlatVol^ Cephei::QL::Models::Marketmodels::Models::CFlatVol_Factory::Create (Cephei::IVector<Double>^ volatilities, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, UInt64 numberOfFactors, Cephei::IVector<Double>^ initialRates, Cephei::IVector<Double>^ displacements)
{
    return gcnew CFlatVol ( volatilities,  corr,  evolution,  numberOfFactors,  initialRates,  displacements);
}
